﻿namespace LeetCodeProject._0001_1000._001_100._11_20._020;

public class Solution
{
    public bool IsValid(string s)
    {
        var stk = new Stack<char>(s.Length);
        foreach (var c in s)
        {
            switch (c)
            {
                case '(':
                case '[':
                case '{':
                    stk.Push(c);
                    break;

                case ')':
                    if (stk.Count == 0 || stk.Pop() != '(')
                        return false;
                    break;

                case ']':
                    if (stk.Count == 0 || stk.Pop() != '[')
                        return false;
                    break;

                case '}':
                    if (stk.Count == 0 || stk.Pop() != '{')
                        return false;
                    break;

                default:
                    throw new ArgumentException();
            }
        }

        return stk.Count == 0;
    }
}